<?php

namespace App\Console\Commands;

use App\Models\Personnel\Personnel;
use App\Models\Personnel\PersonnelFile;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class updatePersonnelFile extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update:file';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新人员资料，身份证';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        // 行政身份证
        $adminPath = public_path('storage/personnel/机关后勤/身份证');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePath($adminFiles, '行政');
        $admin = Personnel::query()->where('type', 0)->get();
        $this->updatePersonIDCard($admin, $adminFiles);
        // 行政附件
        $adminPath = public_path('storage/personnel/机关后勤/附件');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePathAppendix($adminFiles, '行政');
        $this->updatePersonFile($adminFiles, 0);
        // 驾驶员身份证
        $adminPath = public_path('storage/personnel/驾驶员资料扫描件/身份证');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePath($adminFiles, '驾驶员');
        $admin = Personnel::query()->where('type', 1)->get();
        $this->updatePersonIDCard($admin, $adminFiles);
        // 驾驶员附件
        $adminPath = public_path('storage/personnel/驾驶员资料扫描件/资料');
        $adminFiles = File::allFiles($adminPath);
        $adminFiles = $this->getFilesNamePathAppendix($adminFiles, '驾驶员');
        $this->updatePersonFile($adminFiles, 1);
    }

    public function getFilesNamePath($files, $type)
    {
        $data = [];
        $basePath = config('app.url').'/storage/personnel/';
        foreach ($files as $k => $file) {
            $baseName = $file->getBasename();
            $data[] = [
                'name' => explode('.', $baseName)[0],
                'path' => $basePath.$type.'/'.$file->getFileName(),
            ];
        }

        return collect($data);
    }

    public function updatePersonIDCard($personnel, $files)
    {
        foreach ($personnel as $k => $person) {
            $photo = $files->where('name', $person->name)->first();
            if ($photo) {
                $person->update(['IDCard_url' => $photo['path']]);
                $this->info("- Yes: {$photo['name']}");
            } else {
                $this->info("- No: {$person->name}");
            }
        }
    }

    public function getFilesNamePathAppendix($files, $type)
    {
        $data = [];
        $basePath = config('app.url').'/storage/personnel/';
        foreach ($files as $k => $file) {
            $name = $file->getRelativePath();
            $baseName = $file->getBasename();
            $extension = $file->getExtension();
            $pathname = explode('/', $file->getRealPath());
            $size = $file->getSize();
            $storage = $type === '行政' ? '机关后勤/附件/' : '驾驶员资料扫描件/资料/';

            $data[] = [
                'type'      => $type === '行政' ? 'admin' : 'driver',
                'file_type' => $extension,
                'file_size' => $size,
                'file_name' => explode('.', $baseName)[0],
                'file_path' => $basePath.$storage.$name.'/'.$file->getFileName(),
                'user_id'   => 1,
                'name'      => $pathname[count($pathname) - 2],
            ];
        }

        return collect($data);
    }

    public function updatePersonFile($files, $type)
    {
        foreach ($files as $kf => $vf) {
            $photo = Personnel::query()->where('type', $type)->where('name', $vf['name'])->first();
            if ($photo) {
                unset($vf['name']);
                $personnelFile = new PersonnelFile($vf);
                $personnelFile->personnel_id = $photo['id'];
                $personnelFile->save();
                $this->info("- Yes: {$photo['name']}");
            } else {
                $this->info("- No: {$vf['name']}");
            }
        }
    }
}
